import { _decorator, Component, EventTouch, Label, Node, Sprite, SpriteAtlas, SpriteFrame } from 'cc';
import { DataMgr, GameData } from '../../Basic/Manager/DataManager';
import { GameParams } from '../../Basic/Config/DataConfig';
import { GameModel } from './GameModel';
import { GetItemModel } from '../GetItem/GetItemModel';
import { Tool } from '../../Util/Tool';
import { EventManager } from '../../Basic/Manager/EventManager';
import { EventType } from '../Config/GameEnum';
import { ButtonRedefine } from '../../Basic/Component/ButtonRedefine';
const { ccclass, property } = _decorator;

@ccclass('PropItem')
export class PropItem extends Component {
  @property(Sprite)
  itemSpr: Sprite;
  @property(Label)
  itemCount: Label;
  @property(Node)
  itemAdd: Node;
  @property(ButtonRedefine)
  btn: ButtonRedefine;
  curPropId: string;

  prop1: boolean;

  setData(propId: string) {
    this.curPropId = propId;
  }

  protected onEnable(): void {
    this.rendererUI();
  }

  protected onDisable(): void {
    this.node.off(Node.EventType.TOUCH_START, this.onTouchDown);
    this.node.off(Node.EventType.TOUCH_END, this.onTouchUp);
    this.node.off(Node.EventType.TOUCH_CANCEL, this.onTouchUp);
  }

  rendererUI() {
    /** 获取表数据 */
    // const data =
    this.prop1 = false;
    let propKey = null;
    switch (this.curPropId) {
      case 'item_box' /** 蛋糕盒 */:
        propKey = GameParams.item_box;
        this.node.on(Node.EventType.TOUCH_START, this.onTouchDown, this);
        this.node.on(Node.EventType.TOUCH_END, this.onTouchUp, this);
        this.node.on(Node.EventType.TOUCH_CANCEL, this.onTouchUp, this);
        this.btn.enabled = false;
        this.btn.interactable = false;
        break;
      // case 2 /** 彩色蛋糕 */:
      //   propKey = GameParams.prop_2;
      //   break;
      case 'item_scoop1' /** 蛋糕铲(行) */:
        propKey = GameParams.item_scoop1;
        this.btn.enabled = true;
        break;
      case 'item_scoop2' /** 蛋糕铲(列) */:
        propKey = GameParams.item_scoop2;
        this.btn.enabled = true;
        break;
      // case 5 /** 左转 */:
      //   propKey = GameParams.prop_5;
      //   break;
      // case 6 /** 右转 */:
      //   propKey = GameParams.prop_6;
      //   break;
    }
    const count = DataMgr.gameParams.get(propKey);
    this.itemCount.string = `${count}`;
    this.itemAdd.active = count <= 0;

    const colorData = DataMgr.tables.TbColor.get(this.curPropId);
    const imgData = colorData.itemImg === '' ? DataMgr.tables.TbImg.get(this.curPropId) : DataMgr.tables.TbImg.get(colorData.itemImg);
    // Tool.getSpriteByKey(imgData.altasPath, imgData.imgName).then((sp) => {
    //   this.itemSpr.spriteFrame = sp;
    // });
    if (imgData.altasPath === '') {
      GameModel.Ins()
        .loadDynamicRes<SpriteFrame>(imgData.imgName + '/spriteFrame')
        .then((spr) => {
          this.itemSpr.spriteFrame = spr;
        });
    } else {
      GameModel.Ins()
        .loadDynamicRes<SpriteAtlas>(imgData.altasPath)
        .then((altas) => {
          this.itemSpr.spriteFrame = altas.getSpriteFrame(imgData.imgName);
        });
    }
  }

  onTouchDown(t: EventTouch) {
    if (!GameModel.Ins().cellMachine.releaseEnable) return;
    if (this.curPropId === 'item_box' && GameData.get(GameParams.item_box) > 0) {
      const ret = GameModel.Ins().cellMachine.checkColorItem() > 0;
      if (ret) {
        this.prop1 = true;
        EventManager.Ins().emit(EventType.SHOW_MOUSE_CTRL, false);
        // GameModel.Ins().propMac.showAllItem(false);
        GameModel.Ins().cellMachine.showBoxPropItem(true, t.getUILocation());
        this.node.on(Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
        // this.node.on(Node.EventType.TOUCH_END, this.onTouchUp, this);
      }
    }
  }

  onTouchMove(t: EventTouch) {
    GameModel.Ins().cellMachine.showBoxPropItem(true, t.getUILocation());
  }

  onTouchUp(t: EventTouch) {
    this.node.off(Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
    if (GameData.get(GameParams.item_box) <= 0) return this.onClickAdd();
    GameModel.Ins().cellMachine.showBoxPropItem(false);
    // 判断位置
    GameModel.Ins().cellMachine.releaseBoxProp(t.getUILocation());
  }

  onClickProp() {
    if (this.itemCount.string === '0') return this.onClickAdd();
    if (GameModel.Ins().spineItem != null || GameModel.Ins().cellMachine.prop1Enable) return;
    let ret = false;
    switch (this.curPropId) {
      case 'item_box' /** 蛋糕盒 */:
        if (GameData.get(GameParams.item_box) <= 0) break;
        ret = GameModel.Ins().cellMachine.useProp('item_box');
        if (ret) DataMgr.changeGameAttr(GameParams.item_box, -1);
        break;
      // case 2 /** 彩色蛋糕 */:
      //   if (DataMgr.gameParams.get(GameParams.prop_2) <= 0) break;
      //   ret = GameModel.Ins().cellMachine.useProp(2);
      //   if (ret) DataMgr.gameParams.set(GameParams.prop_2, DataMgr.gameParams.get(GameParams.prop_2) - 1);
      //   break;
      case 'item_scoop1' /** 蛋糕铲(行) */:
        if (GameData.get(GameParams.item_scoop1) <= 0) break;
        ret = GameModel.Ins().cellMachine.useProp('item_scoop1');
        if (ret) DataMgr.changeGameAttr(GameParams.item_scoop1, -1);
        break;
      case 'item_scoop2' /** 蛋糕铲(列) */:
        if (GameData.get(GameParams.item_scoop2) <= 0) break;
        ret = GameModel.Ins().cellMachine.useProp('item_scoop2');
        if (ret) DataMgr.changeGameAttr(GameParams.item_scoop2, -1);
        break;
      // case 5 /** 左转 */:
      //   if (DataMgr.gameParams.get(GameParams.prop_5) <= 0) break;
      //   ret = GameModel.Ins().cellMachine.useProp(5);
      //   if (ret) DataMgr.gameParams.set(GameParams.prop_5, DataMgr.gameParams.get(GameParams.prop_5) - 1);
      //   break;
      // case 6 /** 右转 */:
      //   if (DataMgr.gameParams.get(GameParams.prop_6) <= 0) break;
      //   ret = GameModel.Ins().cellMachine.useProp(6);
      //   if (ret) DataMgr.gameParams.set(GameParams.prop_6, DataMgr.gameParams.get(GameParams.prop_6) - 1);
      //   break;
    }
    DataMgr.SaveData('game');
    this.rendererUI();
  }

  onClickAdd() {
    GetItemModel.Ins().curPropId = this.curPropId;
    GetItemModel.Ins().Show();
  }
}
